from abc import ABC, abstractmethod
from typing import List, Dict, Any, Optional

class ModelConfig:
    """模型配置类"""
    def __init__(self, api_key: str, model_name: str = "default", api_base: Optional[str] = None,
                 max_tokens: int = 4096, temperature: float = 0.7, response_format: Optional[str] = None,
                 proxy: Optional[str] = None, timeout: int = 30, max_retries: int = 3,
                 extra_config: Optional[Dict[str, Any]] = None):
        self.api_key = api_key
        self.model_name = model_name
        self.api_base = api_base
        self.max_tokens = max_tokens
        self.temperature = temperature
        self.response_format = response_format
        self.proxy = proxy
        self.timeout = timeout
        self.max_retries = max_retries
        self.extra_config = extra_config or {}

    def validate(self):
        """验证模型配置"""
        if not self.api_key:
            raise ValueError("API密钥不能为空")

class AIModelInterface:
    """AI模型接口"""
    
    def __init__(self, config: ModelConfig):
        self.config = config
    
    def generate_text(
        self, 
        messages: List[Dict[str, str]], 
        system_prompt: Optional[str] = None
    ) -> str:
        """
        生成文本的标准方法
        
        Args:
            messages (List[Dict]): 对话消息列表
            system_prompt (Optional[str]): 系统提示词
        
        Returns:
            str: 生成的文本内容
        """
        raise NotImplementedError("子类必须实现generate_text方法")
    
    def generate_structured_output(
        self, 
        messages: List[Dict[str, str]], 
        output_schema: Dict[str, Any],
        system_prompt: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        生成结构化输出
        
        Args:
            messages (List[Dict]): 对话消息列表
            output_schema (Dict): 输出结构定义
            system_prompt (Optional[str]): 系统提示词
        
        Returns:
            Dict: 结构化输出结果
        """
        raise NotImplementedError("子类必须实现generate_structured_output方法")
    
    def validate_config(self):
        """验证模型配置"""
        if not self.config.api_key:
            raise ValueError("API密钥不能为空")